const mongoose = require('mongoose');
const utils = require('../public/utils')
const userSchema = new mongoose.Schema({
    userName: {
	    type: String,  //---------昵称
        trim: true,
        default: '',
        maxlength: 50,    //长度不超过50字符
    },
    account: {
        type: String,  //----------账号
        trim: true,
        required: true,
        validate: { //自定义验证器
            validator: utils.validPhone,
            message: '`{PATH}` 必须是有效的11位手机号码!'
        },
    },
    password: {
        type: String,  //-------密码
        trim: true,
        required: true
    },
    headUrl: {     //---------头像
        type: String,
        default: '',
    },
    birthday:  {     //-------出生年月
        type: String,
        default: '',
    },
    sexCode: {
	    type: Number,  //------性别(1男，2女，3未知)
        default: 3,
        enum: [1,2,3]
    },
},{timestamps: {createdAt: 'createTime', updatedAt: 'upDateTime'}})

//获取数据总条数
userSchema.statics.getCount = function(cb) {
    this.count((err,num) => {
        if(err) {
            return new Error(err);
        }
        cb(num)
    })
}

const UserModel = mongoose.model('user',userSchema); //创建集合（表）


module.exports = UserModel;